using System.Data;
using System.Data.Common;
using Microsoft.Extensions.Logging;
using Dapper.Abstractions;
using Dapper.Extensions.Hooks;
using Dapper.Extensions.Configuration;

namespace Dapper.Extensions;

public class LoggingDbConnectionDecorator(
	IDbConnectionFactory inner,
	ILogger<IDbConnectionFactory> logger,
	DbLoggingConfiguration config)
	: IDbConnectionFactory
{
	private readonly LoggingHook<Empty> _hook = new(logger ?? throw new ArgumentNullException(nameof(logger)), 
		config ?? throw new ArgumentNullException(nameof(config)));
	private readonly WrappedConnectionFactory<Empty> _wrapped = new(
		() => (DbConnection)inner.CreateConnection(),
		async () => (DbConnection)await inner.CreateConnectionAsync());

	public IDbConnection CreateConnection()
	{
		return _wrapped.CreateConnection(_hook, Empty.Object);
	}

	public async Task<IDbConnection> CreateConnectionAsync()
	{
		return await _wrapped.CreateConnectionAsync(_hook, Empty.Object);
	}
}
